//
//  XCCellForSIngerSection.m
//  TTLestening
//
//  Created by dllo on 16/3/24.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XCCellForSIngerSection.h"
#import "Masonry.h"
#import "XCModelForSectionSinger.h"



#define WIDGHT  self.contentView.frame.size.width
#define HEIGHT  self.contentView.frame.size.height

#define WIDGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface XCCellForSIngerSection ()

@property (nonatomic, strong) UILabel *labelForRank;
@property (nonatomic, strong) UILabel *labelForTitle;
@property (nonatomic, strong) UILabel *labelForHQ;
@property (nonatomic, strong) UILabel *labelForMV;
@property (nonatomic, strong) UILabel *labelForSinger;
@property (nonatomic, strong) UIImageView *likeImage;
@property (nonatomic, strong) UILabel *labelForNumber;
@property (nonatomic, copy) NSString *songName;
@property (nonatomic, copy) NSString *singerName;


@end

@implementation XCCellForSIngerSection

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        
       // self.contentView.backgroundColor = [UIColor greenColor];
        
        [self createSubview];
    }
    return self;

}
- (void)createSubview {


    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.contentView addSubview:self.button];

    self.labelForRank = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForRank];

    
    self.labelForTitle = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForTitle];
    self.labelForTitle.font = [UIFont systemFontOfSize:15];
    
    
    self.labelForHQ = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForHQ];
    self.labelForHQ.text = @"HQ";
    self.labelForHQ.textColor = [UIColor greenColor];
    self.labelForHQ.layer.borderWidth = 1;
    self.labelForHQ.layer.cornerRadius = 8;
    self.labelForHQ.font = [UIFont systemFontOfSize:10];
    self.labelForHQ.textAlignment = NSTextAlignmentCenter;
    self.labelForHQ.layer.borderColor = [UIColor greenColor].CGColor;
    self.labelForHQ.layer.masksToBounds = YES;
    
    
    self.labelForMV = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForMV];
    self.labelForMV.text = @"MV";
    self.labelForMV.textColor = [UIColor blueColor];
    self.labelForMV.layer.borderWidth = 1;
    self.labelForMV.layer.cornerRadius = 8;
    self.labelForMV.font = [UIFont systemFontOfSize:10];
    self.labelForMV.textAlignment = NSTextAlignmentCenter;
    self.labelForMV.layer.borderColor = [UIColor blueColor].CGColor;
    self.labelForMV.layer.masksToBounds = YES;
    
    self.labelForSinger = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForSinger];
    self.labelForSinger.font = [UIFont systemFontOfSize:14];
    self.labelForSinger.textColor = [UIColor grayColor];
    
    self.likeImage = [[UIImageView alloc] init];
    [self.contentView addSubview:self.likeImage];
    self.likeImage.image = [UIImage imageNamed:@"iconfont-xin-1"];
    
    self.labelForNumber = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForNumber];
    self.labelForNumber.textColor = [UIColor grayColor];
    self.labelForNumber.font = [UIFont systemFontOfSize:14];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.button];
    [self.button setImage:[UIImage imageNamed:@"iconfont-xiangxia"] forState:UIControlStateNormal];
    self.button.backgroundColor = [UIColor redColor];
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.backButton];
    self.backButton.backgroundColor = [UIColor clearColor];
    
}

- (void)layoutSubviews {

    [super layoutSubviews];
    
    [self.labelForRank mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.contentView).with.equalTo(@5);
        make.top.mas_equalTo(self.contentView).with.equalTo(@(HEIGHT / 3));
        make.bottom.mas_equalTo(self.contentView).with.equalTo(@(-HEIGHT / 3 ));
        make.width.mas_equalTo(self.labelForRank.mas_height).with.multipliedBy(1. / 1.);
        
    }];
    
    [self.labelForTitle mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.labelForRank.mas_right).with.equalTo(@(10 * WIDGHTT));
        make.top.mas_equalTo(self.contentView).with.equalTo(@ (10 *HEIGHTT));
        //  make.bottom.mas_equalTo(self.labelForSinger.mas_top).equalTo(@(-10 * HEIGHTT));
        make.height.equalTo(@((self.contentView.frame.size.height - 25 * HEIGHTT) / 2));
        //        .with.equalTo(@((self.contentView.frame.size.height - 25) / 2));
        
        CGRect rect = [self.songName boundingRectWithSize:CGSizeMake(0 , 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];

            make.width.mas_equalTo(rect.size.width);

}];
    
    
    [self.labelForSinger mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.labelForRank.mas_right).with.equalTo(@ (10 * WIDGHTT));
        make.top.mas_equalTo(self.labelForTitle.mas_bottom).with.equalTo(@ (5 *HEIGHTT));
        make.bottom.mas_equalTo(self.contentView).equalTo(@(-10 * HEIGHTT));
        
        CGRect rect = [self.singerName boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16]} context:nil];
        
        make.width.mas_equalTo(rect.size.width);
        
    }];
    
    [self.likeImage mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.labelForSinger.mas_right).equalTo(@(10 * WIDGHTT));
        make.centerY.mas_equalTo(self.labelForSinger.mas_centerY);
        make.width.equalTo(@(15 * WIDGHTT));
        make.height.equalTo(self.likeImage.mas_width).multipliedBy(1. / 1.);
        
        
    }];
    
    [self.labelForNumber mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.likeImage.mas_right).equalTo(@(10 * WIDGHTT));
        make.centerY.mas_equalTo(self.labelForSinger.mas_centerY);
        make.width.equalTo(@(WIDGHT / 5));
        make.height.equalTo(@((self.contentView.frame.size.height - 25 * HEIGHTT) / 2));
        
    }];
    
    
    
    [self.labelForHQ mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.labelForTitle.mas_right).equalTo(@(10 * WIDGHTT));
        make.centerY.mas_equalTo(self.labelForTitle.mas_centerY);
        make.width.equalTo(@(20 * WIDGHTT));
        make.height.equalTo(self.labelForHQ.mas_width).multipliedBy(1. / 1.);
        
        
    }];
    
    [self.labelForMV mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.labelForHQ.mas_right).equalTo(@(10 * WIDGHTT));
        make.centerY.mas_equalTo(self.labelForTitle.mas_centerY);
        make.width.equalTo(@(20 * WIDGHTT));
        make.height.equalTo(self.labelForHQ.mas_width).multipliedBy(1. / 1.);
        
        
    }];
    
    
    [self.button mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.right.mas_equalTo(self.contentView).equalTo(@(-10 * WIDGHTT));
        make.top.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView);
        make.width.mas_equalTo(self.button.mas_height).multipliedBy(1. / 1.);
        //  make.top.mas_equalTo(self.contentView).equalTo(@10);
        
    }];
    
    [self.backButton mas_updateConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView);
        make.left.mas_equalTo(self.contentView);
        make.right.mas_equalTo(self.button.mas_left).equalTo(@0);
        
        
    }];
    
}


- (void)passModel:(XCModelForSectionSinger *)model sectionNumber:(NSInteger)rank {

    self.labelForRank.text = [NSString stringWithFormat:@"%ld", (long)rank];
   // NSLog(@"%@", model.name);
    
    self.labelForTitle.text = model.name;
    self.songName = model.name;
    
    self.labelForSinger.text = model.singerName;
    self.singerName = model.singerName;
    
    
    if (model.favorites >= 10000 ) {
        
        self.labelForNumber.text = [NSString stringWithFormat:@"%.1f 万", round(model.favorites) / 10000];
    } else {
    
        self.labelForNumber.text = [NSString stringWithFormat:@"%ld", (long)model.favorites];

    }
    


    
    
    
   // [self.contentView layoutSubviews];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
